﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZOVNP.Model.Abstracts;

namespace ZOVNP.WinView
{
    public partial class CustomerEditControl : UserControl
    {
        private ICustomer _customer;

        public CustomerEditControl(ICustomer customer, bool readOnly)
        {
            InitializeComponent();
            Customer = customer;

            tbxSurname.ReadOnly =
                tbxPhoneNumber.ReadOnly =
                tbxPhoneNumber.ReadOnly =
                tbxLastName.ReadOnly =
                tbxFirstName.ReadOnly =
                tbxEMail.ReadOnly =
                tbxDeliveryAddress.ReadOnly =
                tbxAddPhoneNumber.ReadOnly =
                tbxAdditional.ReadOnly = readOnly;
        }

        public ICustomer Customer
        {
            get { return _customer; }
            set
            {
                _customer = value;

                tbxSurname.Text = _customer != null ? _customer.Surname : string.Empty;
                tbxFirstName.Text = _customer != null ? _customer.FirstName : string.Empty;
                tbxLastName.Text = _customer != null ? _customer.LastName : string.Empty;
                tbxDeliveryAddress.Text = _customer != null ? _customer.DeliveryAddress : string.Empty;
                tbxPhoneNumber.Text = _customer != null ? _customer.PhoneNumber : string.Empty;
                tbxAddPhoneNumber.Text = _customer != null ? _customer.AddPhoneNumber : string.Empty;
                tbxEMail.Text = _customer != null ? _customer.EMail : string.Empty;
                tbxAdditional.Text = _customer != null ? _customer.Additional : string.Empty;
            }
        }
        
        private void tbxSurname_TextChanged(object sender, EventArgs e)
        {
            if(_customer == null)
                throw new InvalidOperationException();

            _customer.Surname = tbxSurname.Text;
        }

        private void tbxFirstName_TextChanged(object sender, EventArgs e)
        {
            if (_customer == null)
                throw new InvalidOperationException();

            _customer.FirstName = tbxFirstName.Text;
        }

        private void tbxLastName_TextChanged(object sender, EventArgs e)
        {
            if (_customer == null)
                throw new InvalidOperationException();

            _customer.LastName = tbxLastName.Text;
        }

        private void tbxDeliveryAddress_TextChanged(object sender, EventArgs e)
        {
            if (_customer == null)
                throw new InvalidOperationException();

            _customer.DeliveryAddress = tbxDeliveryAddress.Text;
        }

        private void tbxPhoneNumber_TextChanged(object sender, EventArgs e)
        {
            if (_customer == null)
                throw new InvalidOperationException();

            _customer.PhoneNumber = tbxPhoneNumber.Text;
        }

        private void tbxAddPhoneNumber_TextChanged(object sender, EventArgs e)
        {
            if (_customer == null)
                throw new InvalidOperationException();

            _customer.AddPhoneNumber = tbxAddPhoneNumber.Text;
        }

        private void tbxEMail_TextChanged(object sender, EventArgs e)
        {
            if (_customer == null)
                throw new InvalidOperationException();

            _customer.EMail = tbxEMail.Text;
        }

        private void tbxAdditional_TextChanged(object sender, EventArgs e)
        {
            if (_customer == null)
                throw new InvalidOperationException();

            _customer.Additional = tbxAdditional.Text;
        } 
    }
}
